<template>
  <a-drawer
    title="调剂"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="CampusAdjustVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form = form>
      <a-alert :showIcon="false" :message="'请选择 '+postParams.ksxm+' 要调剂的院系（专业、方向）'" banner />
      <a-row :gutter="48">
        <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['bkyxsm',{rules:[{required:true,message:'请选择院系'}]},{initialValue:''}]" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+'-'+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['bkzydm',{rules:[{required:true,message:'请选择专业'}]},{initialValue:''}]" @change="zyChange">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+'-'+ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="学习形式">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['zhxlxxxs',{rules:[{required:true,message:'请选择学习形式'}]},{initialValue:''}]"   @change="xsChange">
                <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.bkyjfxm" >
                <a-select-option v-for="i in selectMenu.yjfx" :key="i.id" :value="i.yjfxm">{{i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="是否保留">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.sfbl1" >
                <a-select-option v-for="i in bl" :key="i.id" :value="i.id">{{i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="备注" v-show="postParams.zxjh!=0">
             
               <a-input placeholder="请输入..." v-model="postParams.note" :rows="4"/>
              
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  props: {
    CampusAdjustVisible: {
      default: false
    },
    info:{
      default:() =>({})
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
        bkyjfxm: '',
        bkyjfxmc: '',
        bkyxsm: '',
        bkyxsmc: '',
        zsrs: '',
        bkzydm: '',
        bkzymc: '',
        sfbl1:''
      },
      markNum:'',
      selectMenu: {
        yx: [] ,// 院系
        yjfx:[],
        zy:[],
        xxlx:[]
      },
      bl:[{
        id:'0',
        name:'是'
      },{
        id:'1',
        name:'否'
      }]
    }
  },
    beforeCreate(){
    this.form = this.$form.createForm(this)
  },
  watch:{
    info(){
      this.form.resetFields()
      this.markNum = {...this.info}
      this.postParams = this.info
      this.$set(this.postParams,'sfbl1','1')
        this.form.getFieldDecorator('bkyxsm', {initialValue: this.info['bkyxsm']})
        this.form.getFieldDecorator('bkzydm', {initialValue: this.info['bkzydm']})
        this.form.getFieldDecorator('zhxlxxxs', {initialValue: this.info['zhxlxxxs']})
      if(this.postParams.bkyxsm){

        this.getPression(this.postParams.bkyxsm)
      }
      if(this.postParams.bkzydm){
        this.getDirection(this.postParams.bkzydm)

      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeNoAuth(),
        this.$api.base.dictionaryData('TYPE0441')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data,
          xxlx:res[1].data
        }
      })
    },
    reset () {
      this.data = []
    },
    // 选择院系
    yxChange (code) {
       this.$set(this.postParams, 'bkzydm', '')
      this.$set(this.postParams, 'bkyjfxm', '')
      this.$set(this.selectMenu, 'yjfx', [])
      this.getPression(code)
    },
     // 选择专业
    zyChange(code){
      this.$set(this.postParams, 'bkyjfxm', '')
      this.getDirection(code)
    },
    xsChange(code){
      this.$set(this.postParams, 'bkyjfxm', '')
       this.$api.base.searchDirectory({
        ssyxm: this.form.getFieldValue('bkyxsm'),
        zsxn:this.postParams.zsnj?this.postParams.zsnj:'',
        sszym:this.form.getFieldValue('bkzydm'),
         xwccm: 2,
         qrzhfqrz:code
      }).then(res => {
        this.$set(this.selectMenu, 'yjfx', res.data)
      })
    },
    getPression(code){
       this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    getDirection(code){
      this.$api.base.searchDirectory({
        ssyxm: this.form.getFieldValue('bkyxsm'),
        zsxn:this.postParams.zsnj?this.postParams.zsnj:'',
        sszym: code?code:'',
         xwccm: 2,
         qrzhfqrz:this.postParams.zhxlxxxs 
      }).then(res => {
        this.$set(this.selectMenu, 'yjfx', res.data)
      })
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
      if(this.markNum.bkzydm == this.form.getFieldValue('bkzydm') && this.markNum.bkyxsm == this.form.getFieldValue('bkyxsm') && this.markNum.bkyjfxm == this.postParams.bkyjfxm && this.markNum.zhxlxxxs == this.form.getFieldValue('zhxlxxxs')){
          return this.$message.warning('该学生调剂信息未发生变更，请调整调剂信息')
      }
      var params = {}
      for(let item in this.postParams){
        if(item == 'id'  || item == 'bkyjfxm' || item=='note'){
          params[item] = this.postParams[item]
        }
        if(item == 'sfbl1'){
          params['sfbl'] = this.postParams[item]
        }
      }
      if(this.selectMenu.yjfx){
        this.selectMenu.yjfx.forEach(item =>{
          if(item.yjfxm == this.postParams.bkyjfxm){
            this.postParams.bkyjfxmc = item.yjfxmc
          }
        })
      }
      params.bkyjfxmc = this.postParams.bkyjfxmc
      this.$api.recruitstat.schoolTransferManageUpdate(Object.assign(params,this.form.getFieldsValue())).then( res => {
        this.reset()
        this.$emit('success', this.postParams)
      })
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
